define(["angularAMD"], function (angularAMD) {
    angularAMD.service('QT07_LoadQueryService', ['$state','$rootScope', '$filter', '$stateParams', 'config', '$mdDialog', 'MMWService', '$timeout', '$q',
        function ($state, $rootScope, $filter, $stateParams, config, $mdDialog, $MMWService, $timeout, $q) {

            var service = this;
            
            service.init = (option, call_back) => {
                openDialog(option, call_back);
            };

            function openDialog(option, call_back) {
                $mdDialog.dialog(
                    "module/moduleQT/program/QT07/dialog/QT07_LoadQueryService.html",
                    function (dialog) {
                        return {
                            css_url:
                                "module/moduleQT/program/QT07/dialog//QT07_LoadQueryService.css",
                            templateList: [],
                            modelName: "",
                            traceModelList: [],
                            traceModel: {},
                            beforeShown: async () => {
                                // 1.6	S2点击按钮，打开S4界面
                                // 展示当前账号之前保存的筛选条件模板，用户可自由调出，快速使用
                                const resp = await loadModel();
                                // 20220817 modify by Alan for#113984 : 修正列表只剩一項刪除時，畫面沒有更新
                                $timeout(() => {
                                    dialog.traceModelList = resp.TraceModelList;
                                });
                            },
                            back() {
                                dialog.hide();
                            },
                            delete: (modelName) => {
                                // 1.6.1	S4点击按钮
                                // 点击后删除对应模板在表中的数据
                                $rootScope.showConfirm(
                                    $filter("translate")(
                                        "QT_standard.common.loadModule.msg.whether_delete_this_module"
                                    ),
                                    async (confirmDialog) => {
                                        await deleteModel(modelName);
                                        confirmDialog.hide();
                                        await dialog.beforeShown();
                                    }
                                );
                            },
                            select: (template) => {
                                dialog.modelName =
                                    dialog.modelName === template.MODELNAME
                                        ? ""
                                        : template.MODELNAME;
                            },
                            confirm: () => {
                                if (!dialog.modelName) {
                                    return;
                                }
                                dialog.hide();
                                // 1.6.2	S4点击按钮
                                // 选择相关模板名称后，将模板内容加载到S2界面中，如已界面上已有数据，删除界面上的数据，并将模板内的数据展示出来。

                                getTraceModel(dialog.modelName).then((resp) => {
                                    if (resp.TraceModel && resp.TraceModel.length > 0) {
                                        dialog.traceModel = resp.TraceModel;
                                        call_back && call_back(dialog);
                                    }
                                });

                            },
                        };
                    }
                );
            };

            const service_QTR_Step1 = (content) => {
                return new Promise(async (resolve, reject) => {
                    let result;
                    $MMWService.sendToServer({
                        uri: "ServicesSTD.Module_LEAN.QTR_Step1",
                        content: content,
                        success: (data) => (result = resolve(data)),
                    });

                    return result;
                });
            };

            const loadModel = async () => {
                const content = { Name: "GetTraceModelList" };
                const resp = await service_QTR_Step1(content);
                return resp;
            };

            const deleteModel = async (modelName) => {
                const content = {
                    Name: "DeleteTraceModel",
                    MODELNAME: modelName,
                };
                const resp = await service_QTR_Step1(content);
                return resp;
            };

            const getTraceModel = async (modelName) => {
                const content = {
                    Name: "GetTraceModel",
                    MODELNAME: modelName,
                };
                const resp = await service_QTR_Step1(content);
                return resp;
            };
        }
    ])
});
